/*
 * Created on Jun 24, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.peterrussell.contractionTimer.controller;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

import org.peterrussell.contractionTimer.model.ContractionTimeDTO;
import org.peterrussell.contractionTimer.view.About;
import org.peterrussell.contractionTimer.view.MIDletSingleton;
import org.peterrussell.contractionTimer.view.MainCanvas;

/**
 * @author peterr
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class LCDController implements UIController {

	private final int START = 1;
	private final int STOP = 2;
		
	private Display display;
    private MainCanvas canvas;
    private About about;
    private int buttonStatus;
	/**
	 * 
	 */
	public LCDController() {
		canvas = new MainCanvas();
		about = new About();
		display = Display.getDisplay(MIDletSingleton.getMIDlet());
	}
	
	public void welcome() {
        display.setCurrent(about);
	}
	
	public void mainTimer() {
        display.setCurrent(canvas);
        this.buttonStatus = START;
	}
	
	public void exit() {
		MIDlet app = MIDletSingleton.getMIDlet();
        app.notifyDestroyed();

	}
	
	/* (non-Javadoc)
	 * @see org.peterrussell.contractionTimer.UIController#startContractionTimer()
	 */
	public void startContractionTimer() {
		if (buttonStatus == START) {
	        canvas.removeCommand(canvas.startCommand);
            canvas.addCommand(canvas.stopCommand);
            buttonStatus = STOP;
		}
		canvas.setCurrentTimer(Controller.CONTRACTION);
		canvas.repaint();
	}

	/* (non-Javadoc)
	 * @see org.peterrussell.contractionTimer.UIController#stopContractionTimer()
	 */
	public void stopContractionTimer() {
		canvas.repaint();
	}

	/* (non-Javadoc)
	 * @see org.peterrussell.contractionTimer.UIController#refreshContractionTimer()
	 */
	public void refreshContractionTimer(ContractionTimeDTO dto) {
		canvas.setContractionTime(dto.getContractionTime());
		// TODO: restrict refresh area to only that requiring repaint
		canvas.repaint();
	}

	/* (non-Javadoc)
	 * @see org.peterrussell.contractionTimer.UIController#startBetweenContractionTimer()
	 */
	public void startBetweenContractionTimer() {
		canvas.setCurrentTimer(Controller.BETWEEN_CONTRACTION);
	}

	/* (non-Javadoc)
	 * @see org.peterrussell.contractionTimer.UIController#stopBetweenContractionTimer()
	 */
	public void stopBetweenContractionTimer() {
		
	}

	/* (non-Javadoc)
	 * @see org.peterrussell.contractionTimer.UIController#refreshBetweenContractionTimer()
	 */
	public void refreshBetweenContractionTimer(ContractionTimeDTO dto) {
		canvas.setBetweenContractionTime(dto.getBetweenContractionTime());
		// TODO: restrict refresh area to only that requiring repaint
		canvas.repaint();
	}
	
	
	/* (non-Javadoc)
	 * @see org.peterrussell.contractionTimer.controller.UIController#refreshAverages(org.peterrussell.contractionTimer.model.ContractionTimeDTO[])
	 */
	public void refreshAverages(ContractionTimeDTO[] timeArray) {
		canvas.setTimeArray(timeArray);
		// TODO: restrict refresh area to only that requiring repaint
		canvas.repaint();
	}
}
